package com.css.gdzj.gdzjbxgd.modules.exportWord;

import com.css.gdzj.gdzjbxgd.common.util.R;
import com.css.gdzj.gdzjbxgd.common.util.ServiceUtil;
import com.css.gdzj.gdzjbxgd.modules.reportinfo.repository.entity.BxgdReportInfoEntity;
import com.css.gdzj.gdzjbxgd.modules.reportinfo.repository.entity.BxgdReportInfoEntityVo;
import com.css.gdzj.gdzjbxgd.modules.reportinfo.repository.entity.HonestGiftChildEntity;
import com.css.gdzj.gdzjbxgd.modules.reportinfo.repository.entity.HonestPtjobChildEntity;
import com.css.gdzj.gdzjbxgd.modules.userinfo.repository.entity.UserInfoEntity;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.policy.HackLoopTableRenderPolicy;
import org.jodconverter.DocumentConverter;
import org.jodconverter.document.DefaultDocumentFormatRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
public class ExportWordPoiController {

    /**
     * 根据word模板生成word文档
     * @param request
     * @param response
     */

    @Autowired
    private ServiceUtil serviceUtil;

    @Autowired
    private DocumentConverter converter;

    @PostMapping( "/preViewPdf")
    @ResponseBody
    public void preViewPdf(@RequestBody BxgdReportInfoEntityVo bxgdReportInfoVo, HttpServletRequest request,HttpServletResponse response){
        try {
            String tempFile="/data/word/ylTemplate.docx";
            //String tempFile="D:/ylTemplate.docx";
            BxgdReportInfoEntity bxgdReportInfo = bxgdReportInfoVo.getBxgdReportInfoEntity();
            //小表 gift
            List<HonestGiftChildEntity> giftReport = bxgdReportInfoVo.getHonestGiftChildEntityList();
            //小表 ptjob
            List<HonestPtjobChildEntity> ptjobReport = bxgdReportInfoVo.getHonestPtjobChildEntityList();
            R userInfoByRequest = serviceUtil.getUserInfoByRequest(request);
            UserInfoEntity userInfo = (UserInfoEntity)userInfoByRequest.get("data");
            String surveyFour = bxgdReportInfo.getSurveyFour();
            String four = "0".equals(surveyFour) ? "☑ 未开展督导调研" : "囗未开展督导调研";
            bxgdReportInfo.setSurveyFour(four);
            String surveyOne = bxgdReportInfo.getSurveyOne();
            switch (surveyOne){
                case "0":
                   surveyOne="☑是 囗否" ;
                   break;
                case "1":
                    surveyOne="囗是 ☑否";
                    break;
                default:
                    surveyOne="囗是 囗否";
            }
            bxgdReportInfo.setSurveyOne(surveyOne);
            String surveyTwo = bxgdReportInfo.getSurveyTwo();
            switch (surveyTwo){
                case "0":
                    surveyTwo="☑是 囗否" ;
                    break;
                case "1":
                    surveyTwo="囗是 ☑否";
                    break;
                default:
                    surveyTwo="囗是 囗否";
            }
            bxgdReportInfo.setSurveyTwo(surveyTwo);

            String surveyThree = bxgdReportInfo.getSurveyThree();
            switch (surveyThree){
                case "0":
                    surveyThree="☑是 囗否" ;
                    break;
                case "1":
                    surveyThree="囗是 ☑否";
                    break;
                default:
                    surveyThree="囗是 囗否";
            }
            bxgdReportInfo.setSurveyThree(surveyThree);

            String increaseOne = bxgdReportInfo.getIncreaseOne();
            switch (increaseOne){
                case "0":
                    increaseOne="☑基本做到 □部分做到,有待改进 □未做到";
                    break;
                case "1":
                    increaseOne="□基本做到 ☑部分做到,有待改进 □未做到";
                    break;
                default:
                    increaseOne="□基本做到 □部分做到,有待改进 ☑未做到";
            }
            bxgdReportInfo.setIncreaseOne(increaseOne);

            String increaseTwo = bxgdReportInfo.getIncreaseTwo();
            switch (increaseTwo){
                case "0":
                    increaseTwo="☑是 □否 □未出席" ;
                    break;
                case "1":
                    increaseTwo="□是 ☑否 □未出席";
                    break;
                default:
                    increaseTwo="□是 □否 ☑未出席";
            }
            bxgdReportInfo.setIncreaseTwo(increaseTwo);


            String increaseThree = bxgdReportInfo.getIncreaseThree();
            switch (increaseThree){
                case "0":
                    increaseThree="☑基本做到 □部分做到,有待改进 □未做到";
                    break;
                case "1":
                    increaseThree="□基本做到 ☑部分做到,有待改进 □未做到";
                    break;
                default:
                    increaseThree="□基本做到 □部分做到,有待改进 ☑未做到";
            }
            bxgdReportInfo.setIncreaseThree(increaseThree);


            String visitingOne = bxgdReportInfo.getVisitingOne();
            visitingOne = "0".equals(visitingOne) ? "☑是 囗否" : "囗是 ☑否";
            bxgdReportInfo.setVisitingOne(visitingOne);

            String visitingTwo = bxgdReportInfo.getVisitingTwo();
            visitingTwo = "0".equals(visitingTwo) ? "☑是 囗否" : "囗是 ☑否";
            bxgdReportInfo.setVisitingTwo(visitingTwo);

            String visitingThree = bxgdReportInfo.getVisitingThree();
            visitingThree = "0".equals(visitingThree) ? "☑是 囗否" : "囗是 ☑否";
            bxgdReportInfo.setVisitingThree(visitingThree);

            String visitingFour = bxgdReportInfo.getVisitingFour();
            visitingFour = "0".equals(visitingFour) ? "☑是 囗否" : "囗是 ☑否";
            bxgdReportInfo.setVisitingFour(visitingFour);

            String visitingFive = bxgdReportInfo.getVisitingFive();
            visitingFive = "0".equals(visitingFive) ? "☑是 囗否" : "囗是 ☑否";
            bxgdReportInfo.setVisitingFive(visitingFive);

            String visitingSix = bxgdReportInfo.getVisitingSix();
            visitingSix = "0".equals(visitingSix) ? "☑是 囗否" : "囗是 ☑否";
            bxgdReportInfo.setVisitingSix(visitingSix);

            String saveReceptionOne = bxgdReportInfo.getSaveReceptionOne();
            switch (saveReceptionOne){
                case "0":
                    saveReceptionOne="☑是 □否 □未参加公务接待和被接待" ;
                    break;
                case "1":
                    saveReceptionOne="□是 ☑否 □未参加公务接待和被接待";
                    break;
                default:
                    saveReceptionOne="□是 □否 ☑未参加公务接待和被接待";
            }
            bxgdReportInfo.setSaveReceptionOne(saveReceptionOne);


            String saveCarOne = bxgdReportInfo.getSaveCarOne();
            switch (saveCarOne){
                case "0":
                    saveCarOne="☑是 囗否" ;
                    break;
                default:
                    saveCarOne="囗是 ☑否";
            }
            bxgdReportInfo.setSaveCarOne(saveCarOne);

            String saveCarTwo = bxgdReportInfo.getSaveCarTwo();
            switch (saveCarTwo){
                case "0":
                    saveCarTwo="☑是 囗否" ;
                    break;
                case "1":
                    saveCarTwo="囗是 ☑否";
                    break;
                default:
                    saveCarTwo="囗是 囗否";
            }
            bxgdReportInfo.setSaveCarTwo(saveCarTwo);

            String saveCarThree = bxgdReportInfo.getSaveCarThree();
            saveCarThree = "0".equals(saveCarThree) ? "☑未配备公车" : "囗未配备公车";
            bxgdReportInfo.setSaveCarThree(saveCarThree);


            String saveConsumeOne = bxgdReportInfo.getSaveConsumeOne();
            saveConsumeOne = "0".equals(saveConsumeOne) ? "☑是 囗否" : "囗是 ☑否";
            bxgdReportInfo.setSaveConsumeOne(saveConsumeOne);

            String saveConsumeTwo = bxgdReportInfo.getSaveConsumeTwo();
            saveConsumeTwo = "0".equals(saveConsumeTwo) ? "☑是 囗否" : "囗是 ☑否";
            bxgdReportInfo.setSaveConsumeTwo(saveConsumeTwo);

            String saveConsumeThree = bxgdReportInfo.getSaveConsumeThree();
            saveConsumeThree = "0".equals(saveConsumeThree) ? "☑是 囗否" : "囗是 ☑否";
            bxgdReportInfo.setSaveConsumeThree(saveConsumeThree);

            String saveConsumeFour = bxgdReportInfo.getSaveConsumeFour();
            saveConsumeFour = "0".equals(saveConsumeFour) ? "☑是 囗否" : "囗是 ☑否";
            bxgdReportInfo.setSaveConsumeFour(saveConsumeFour);

/*            String saveMarry = bxgdReportInfo.getSaveMarry();
            saveMarry = "1".equals(saveMarry) ? "☑操办" : "囗操办";
            bxgdReportInfo.setSaveMarry(saveMarry);*/


            String saveMarryOne = bxgdReportInfo.getSaveMarryOne();
            switch (saveMarryOne){
                case "0":
                    saveMarryOne="☑是 囗否" ;
                    break;
                case "1":
                    saveMarryOne="囗是 ☑否";
                    break;
                default:
                    saveMarryOne="囗是 囗否";
            }
            bxgdReportInfo.setSaveMarryOne(saveMarryOne);


            String saveMarryTwo = bxgdReportInfo.getSaveMarryTwo();
            switch (saveMarryTwo){
                case "0":
                    saveMarryTwo="☑是 囗否" ;
                    break;
                case "1":
                    saveMarryTwo="囗是 ☑否";
                    break;
                default:
                    saveMarryTwo="囗是 囗否";
            }
            bxgdReportInfo.setSaveMarryTwo(saveMarryTwo);

            String saveMarry2 = bxgdReportInfo.getSaveMarry();
            saveMarry2 = "0".equals(saveMarry2) ? "☑未操办" : "囗未操办";
            bxgdReportInfo.setSaveMarry(saveMarry2);
            if(ptjobReport.size()>0&&ptjobReport!=null){
                for(HonestPtjobChildEntity pt: ptjobReport){
                    String honestPtjobMoney="";
                    switch (pt.getHonestPtjobMoney()){
                        case "0":
                            honestPtjobMoney="☑是 囗否" ;
                            break;
                        case "1":
                            honestPtjobMoney="囗是 ☑否";
                            break;
                        default:
                            honestPtjobMoney="囗是 囗否";
                    }
                    pt.setHonestPtjobMoney(honestPtjobMoney);
                }
            }else{
                ptjobReport= new ArrayList<>();
                ptjobReport.add(new HonestPtjobChildEntity());
                ptjobReport.get(0).setHonestPtjobUnit("");
                ptjobReport.get(0).setHonestPtjobPost("");
                ptjobReport.get(0).setHonestPtjobApproval("");
                ptjobReport.get(0).setHonestPtjobMoney("囗是 囗否");
            }




            String honestPtjob = bxgdReportInfo.getHonestPtjob();
            honestPtjob = "0".equals(honestPtjob) ? "☑未兼职 " : "囗未兼职";
            bxgdReportInfo.setHonestPtjob(honestPtjob);


            String honestTravelOne = bxgdReportInfo.getHonestTravelOne();
            honestTravelOne = "0".equals(honestTravelOne) ? "☑是 囗否" : "囗是 ☑否";
            bxgdReportInfo.setHonestTravelOne(honestTravelOne);


            String honestTravelTwo = bxgdReportInfo.getHonestTravelTwo();
            honestTravelTwo = "0".equals(honestTravelTwo) ? "☑是 囗否" : "囗是 ☑否";
            bxgdReportInfo.setHonestTravelTwo(honestTravelTwo);

            if(giftReport.size()>0&&giftReport!=null){
                for(HonestGiftChildEntity gift: giftReport){
                    String honestGiftHandle="";
                    switch (gift.getHonestGiftHandle()){
                        case "0":
                            honestGiftHandle="☑退还 囗上缴 囗收受" ;
                            break;
                        case "1":
                            honestGiftHandle="囗退还 ☑上缴 囗收受";
                            break;
                        default:
                            honestGiftHandle="囗退还 囗上缴 ☑收受";
                    }
                    gift.setHonestGiftHandle(honestGiftHandle);
                }

            }else{
                giftReport= new ArrayList<>();
                giftReport.add(new HonestGiftChildEntity());
                giftReport.get(0).setHonestGiftName("");
                giftReport.get(0).setHonestGiftAmount("");
                giftReport.get(0).setHonestGiftHandle("囗退还 囗上缴 囗收受");
            }

            String honestGift = bxgdReportInfo.getHonestGift();
            honestGift = "0".equals(honestGift) ? "☑未遇到管理服务对象赠送礼品礼金情况" : "囗未遇到管理服务对象赠送礼品礼金情况";
            bxgdReportInfo.setHonestGift(honestGift);
            //绑定表格
            HackLoopTableRenderPolicy policy = new HackLoopTableRenderPolicy();

            Configure config = Configure.builder()
                    .bind("ptjob", policy).bind("gift", policy).build();

            List<HonestGiftChildEntity> finalGiftReport = giftReport;
            List<HonestPtjobChildEntity> finalPtjobReport = ptjobReport;
            XWPFTemplate template = XWPFTemplate.compile(tempFile,config).render(
                    new HashMap<String, Object>() {{
                        put("vo",bxgdReportInfo);
                        put("gift", finalGiftReport);
                        put("ptjob", finalPtjobReport);
                        put("userInfo",userInfo);
                    }});
            ByteArrayOutputStream byteArrayOutputStream1 = new ByteArrayOutputStream();
            BufferedOutputStream bos = new BufferedOutputStream(byteArrayOutputStream1);
            //使用response,将pdf文件以流的方式发送的前段
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/pdf");
            response.setHeader("Content-Disposition", "attachment;filename=" + new String("UserInfoTest.pdf".getBytes("utf-8"), "ISO8859-1"));
            ServletOutputStream outputStream = response.getOutputStream();
            template.write(bos);
            template.close();
            bos.flush();
            bos.close();
            byte[] bytes = byteArrayOutputStream1.toByteArray();
            converter.convert(new ByteArrayInputStream(bytes))
                    .as(DefaultDocumentFormatRegistry.getFormatByExtension("docx"))
                    .to(outputStream)
                    .as(DefaultDocumentFormatRegistry.getFormatByExtension("pdf")).execute();
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
